/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.modules.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.entity.BannerEntity;
import io.renren.modules.app.entity.VersionRoleDto;
import io.renren.modules.app.service.BannerService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 各版本角色操作
 * @author limeng
 * @time 2018/11/1
 */
@RestController
@RequestMapping("/app/vr")
public class VersionRoleController extends AbstractController {
	@Resource
	private BannerService bannerService;

	
	/**
	 * 所有banner列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("app:banner:list")
	public R list(){
		List<VersionRoleDto> versionRoleDtoList = new ArrayList<>();
		for (int i  = 0;i<20;i++){
			VersionRoleDto versionRoleDto = new VersionRoleDto();
			versionRoleDto.setRoleId("p2312j312kj312k"+i);
			versionRoleDto.setVersionId("123l12k2312k3k"+i);
			versionRoleDto.setVrId(""+i);
			versionRoleDto.setAppType("我的南京");
			versionRoleDto.setDeviceType("安卓");
			versionRoleDto.setRoleName("角色名");
			versionRoleDto.setVersionNum("2.9."+i);
			versionRoleDto.setVrName(versionRoleDto.getAppType()+"-"+versionRoleDto.getDeviceType()+"-"+versionRoleDto.getVersionNum()+"-"+versionRoleDto.getRoleName());
			versionRoleDtoList.add(versionRoleDto);
		}
		Page<VersionRoleDto> page = new Page<>();
		page=page.setRecords(versionRoleDtoList);
		PageUtils pageUtils= new PageUtils(page);
		return R.ok().put("page",pageUtils);
	}

	
	/**
	 * 新增
	 */
	@SysLog("新增角色版本")
	@PostMapping("/save")
	@RequiresPermissions("app:banner:save")
	public R save(@RequestBody BannerEntity bannerEntity){
		//数据校验
		verifyForm(bannerEntity);

        bannerService.insert(bannerEntity);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改菜单")
	@PostMapping("/update")
	@RequiresPermissions("app:banner:update")
	public R update(@RequestBody BannerEntity bannerEntity){
		//数据校验
		verifyForm(bannerEntity);
		bannerService.update(bannerEntity);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除菜单")
	@PostMapping("/delete/{menuId}")
	@RequiresPermissions("app:banner:delete")
	public R delete(@RequestBody BannerEntity bannerEntity){
		bannerService.delete(bannerEntity);
		return R.ok();
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(BannerEntity bannerEntity){
		if(StringUtils.isBlank(bannerEntity.getTitle())){
			throw new RRException("banner标题不能为空");
		}
		if(StringUtils.isBlank(bannerEntity.getKey())){
			throw new RRException("菜单码不能为空");
		}
		if(StringUtils.isBlank(bannerEntity.getAndroidImg())){
			throw new RRException("安卓图片不能为空");
		}
		if(StringUtils.isBlank(bannerEntity.getTitle())){
			throw new RRException("IOS图片不能为空");
		}

	}
}
